package com.okilo.online.app.common

import com.okilo.online.app.BuildConfig
import com.okilo.online.app.OkiloApplication
import com.okilo.online.app.base.OkiloBaseFmt
import com.okilo.online.app.event.OkiloEventData
import com.okilo.online.app.event.OkiloEventSubscriber
import com.okilo.online.app.request.HttpHelper
import com.okilo.online.app.request.IHttpCallback
import org.json.JSONObject

/**
 * CommonFragment
 * @Author wwdeng
 * @CreateTime: 2024/11/28 10:32
 * @Annotation：
 */
abstract class OkiloComFmt: OkiloBaseFmt(), OkiloEventSubscriber, IOkiloComView {
    //是否记录埋点
    private var isPageViewRecorded = false

    override fun getAct(): OkiloComAct {
        return context as OkiloComAct
    }

    override fun onStart() {
        super.onStart()
//        HttpHelper.setTag(this)
        OkiloApplication.app.publisher.register(this)
    }

    /**
     * 客服
     */
    fun showCustomerDialog(eventRemark: String = ""){
        getAct().sOkiloAppServiceDialog(eventRemark)
    }

    /**
     * 错误提示
     */
    fun showErrorTipsDialog(tips: String,eventRemark: String = ""){
        getAct().sOkiloAppErrorDialog(tips,eventRemark)
    }

    /**
     * post
     */
    fun doPostAction(httpUrl: String, params: JSONObject, callback: IHttpCallback){
        HttpHelper.post(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                callback.onRequestSuccess(data)
            }

            override fun onRequestError(error: JSONObject) {
                callback.onRequestError(error)
                showOkiloToast(error.optString("errorMsg"))
            }

            override fun onRequestFinish() {
                callback.onRequestFinish()
            }
        })
    }

    /**
     * 添加埋点
     */
    fun addEventPointRequestAction(eventName: String, eventRemark: String = "") {
        HttpHelper.addPoint(eventName,eventRemark)
    }

    /**
     * 发送事件通知
     */
    fun publishEventData(eventData: OkiloEventData){
        OkiloApplication.app.publisher.publish(eventData)
    }

    /**
     * 处理订阅事件
     */
    override fun onOkiloPublishEvent(event: OkiloEventData) {
    }

    override fun onResume() {
        super.onResume()
        if (!isPageViewRecorded) {
            isPageViewRecorded = true
            trackOkiloPageStart()
        }
    }

    override fun onPause() {
        super.onPause()
        if (isRemoving){
            isPageViewRecorded = false
            trackOkiloPageEnd()
        }
    }
    override fun onDestroy() {
        super.onDestroy()
        HttpHelper.cancelTag(this)
        OkiloApplication.app.publisher.detach(this)
    }

    /**
     * 启动埋点
     */
    private fun trackOkiloPageStart() {
        if (enablePageTrack){
            addEventPointRequestAction("${BuildConfig.appName}_${pageName}_start".uppercase())
        }
    }

    /**
     * 关闭埋点
     */
    private fun trackOkiloPageEnd() {
        if (enablePageTrack){
            addEventPointRequestAction("${BuildConfig.appName}_${pageName}_end".uppercase())
        }
    }
}